package com.fastwire.carriere.rad.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.fastwire.carriere.dao.DAOException;
import com.fastwire.carriere.rad.entity.Rad;

/*
 * Creted on 20120225
 * 
 * @author: Lei Jun
 */
public class RadDaoImp extends AbstractRadDaoImp implements RadDao
{    
    @Override
    public void saveOrUpdate(Rad rad) throws DAOException
    {
        super.saveOrUpdate(rad);
    }

    @Override
    public Rad getRadByIP(String ip) throws DAOException
    {
        DetachedCriteria criteria = DetachedCriteria.forClass(Rad.class);

        criteria = criteria.add(getStatusCriteria()).add(Restrictions.eq("ipAddr", ip));
        criteria.setFetchMode("termPoints", FetchMode.JOIN);
        
        return (Rad) (getHibernateTemplate().findSingleObjByCriteria(criteria, false, false));
    }

    @Override
    public List<Rad> getRadByNeName(String name) throws DAOException
    {
        DetachedCriteria criteria = DetachedCriteria.forClass(Rad.class);

        criteria = criteria.add(getStatusCriteria()).add(Restrictions.eq("neID", name));
        criteria.setFetchMode("termPoints", FetchMode.JOIN);

        //criteria.setProjection(Projections.distinct(Projections.property("oid")));
        criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        
        return getHibernateTemplate().findObjsByCriteria(criteria, true, false);
    }
    
    @Override
    public void invalidate(Rad rad) throws DAOException
    {        
        rad.invalidate();

        saveOrUpdate(rad);
    }

    @Override
    public void saveOrUpdate(List<Rad> rads) throws DAOException
    {
        super.saveOrUpdateAllRad(rads);
    }
    
    /*
     * (non-Javadoc)
     * 
     * @see com.fastwire.carriere.dao.RadDao#getAllRadIP()
     */
    // get all existed RAD
    @Override
    public List<String> getAllRadIPs() throws DAOException
    {
        return super.getAllValuesOfOneFiled("SELECT unique(ipAddr) FROM ce_rad WHERE status='active'");   
    }
 
}
